package com.etc.RCCPA002.service.imp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.session.data.redis.RedisOperationsSessionRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.etc.RCCPA002.dao.AdminBannerMapper;
import com.etc.RCCPA002.service.IAdminBannerService;
import com.etc.RCCPA002.vo.BannerVo;
import com.etc.common.Code;
import com.etc.common.ServiceException;
/**
 * 
 * Filename:    AdminBannerServcieImp.java  
 * Description: banner功能服务类   
 * Copyright:   Copyright (c) 2018-2019 All Rights Reserved.
 * Company:     chinasofti.com Inc.
 * @author:     mazheng 
 * @version:    1.0  
 * Create at:   2020年3月3日 下午8:28:03  
 *  
 * Modification History:  
 * Date         Author      Version     Description  
 * ------------------------------------------------------------------  
 * 2020年3月3日      mazheng      1.0         1.0 Version  
 *
 */
@SuppressWarnings({"rawtypes", "unchecked"})
@Service
public class AdminBannerServcieImp implements IAdminBannerService {

    
    @Autowired
    AdminBannerMapper mapper;
    
    /**
     * 获取所有model
     */
    @Override
    public List<Map> findAllModel() {
        return mapper.findAllModel();
    }

    /**
     * 根据条件获取所有banner
     */
    @Override
    public List<Map> findAllBannerList(BannerVo params) {

        
        return mapper.findAllBanner(params);
    }

    /**
     * 根据ID修改状态
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public Map updatestatus(Integer id, Integer statflg) throws ServiceException {

        int i = mapper.updateStatflg(id,statflg);
        if (i != 1) {
            throw new ServiceException(Code.ERROR);
        }
        Map map = mapper.selectById(id);
        return map;

    }

    /**
     * 根据ID修改数据
     */
    @Transactional(rollbackFor=Exception.class)
    @Override
    public Map updatesById(HashMap data) throws ServiceException {
        

        int r = mapper.updateByPrimaryKeySelective(data);
        if (r != 1) {
            throw new ServiceException(Code.ERROR, "更新失败");
        }
        
        Map map = mapper.selectByPrimaryKey(data);
        return map;
    }
    /**
     * 根据id 删除一条记录
     */
    @Transactional(rollbackFor=Exception.class, isolation=Isolation.SERIALIZABLE, propagation=Propagation.REQUIRED)
    @Override
    public void deleteById(Integer id) throws ServiceException {
        int r = mapper.deleteByPrimaryKey(id);
        if (r != 1) {
            throw new ServiceException(Code.ERROR, "删除失败");
        }
        
    }
    /**
     * 根据 ids 数组 删除 数据
     */
    @Transactional(rollbackFor=Exception.class, isolation=Isolation.SERIALIZABLE, propagation=Propagation.REQUIRED)
    @Override
    public void deleteByIds(int[] ids) throws ServiceException {
        
        for (int id: ids) {
            deleteById(id);
        }
    }
    /**
     * 插入数据
     */
    @Transactional(rollbackFor=Exception.class)
    @Override
    public void createBanner(HashMap data) throws ServiceException {
        Integer cid = mapper.findUidByPhone(String.valueOf(data.get("username")));
        data.put("cid", cid);
        int r = mapper.insertSelective(data);
        if (r != 1) {
            throw new ServiceException(Code.ERROR, "插入失败");
        }
    }


}
